package com.ruoyi.web.controller.airplane;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.TbDeviceAndInfoDetailVO;
import com.ruoyi.system.domain.vo.TbDeviceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbDevice;
import com.ruoyi.system.service.ITbDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备信息Controller
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@Api(tags = "设备信息管理")
@RestController
@RequestMapping("/system/device")
public class TbDeviceController extends BaseController {
    @Autowired
    private ITbDeviceService tbDeviceService;


    @ApiOperation("获取设备信息列表分页")
    @GetMapping("/list")
    public TableDataInfo listpage(TbDeviceVO vo) {
        startPage();
        List<TbDeviceVO> list = tbDeviceService.selectTbDeviceLis(vo);
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    @ApiOperation("导出设备信息列表")
    // @PreAuthorize("@ss.hasPermi('system:device:export')")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbDevice tbDevice) {
        List<TbDevice> list = tbDeviceService.selectTbDeviceList(tbDevice);
        ExcelUtil<TbDevice> util = new ExcelUtil<TbDevice>(TbDevice.class);
        util.exportExcel(response, list, "设备信息数据");
    }

    /**
     * 获取设备信息详细信息
     */
//    @ApiOperation("获取设备信息详细信息")
//    // @PreAuthorize("@ss.hasPermi('system:device:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id) {
//        return success(tbDeviceService.selectTbDeviceById(id));
//    }


    @ApiOperation("获取设备信息详细信息")
    // @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/{id}")
    public R<TbDeviceAndInfoDetailVO> getInfo(@PathVariable("id") Long id) {
        return  tbDeviceService.selectTbDeviceInfosById(id);
    }

    /**
     * 新增设备信息
     */
    @ApiOperation("新增设备信息")
    //   @PreAuthorize("@ss.hasPermi('system:device:add')")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbDevice tbDevice) {
      TbDevice device =  tbDeviceService.selectTbDeviceByDevSn(tbDevice.getDevSn());
      if(device != null){
          return error("设备SN号已存在,不能添加");
      }
        tbDevice.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        tbDevice.setCreateTime(DateUtils.getNowDate());
        tbDevice.setRegistrationTime(DateUtils.getNowDate());
        tbDevice.setRecordStatus(1);
        tbDevice.setRegistrationStatus(0);
        return toAjax(tbDeviceService.insertTbDevice(tbDevice));
    }

    /**
     * 修改设备信息
     */
    @ApiOperation("修改设备信息")
    // @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbDevice tbDevice) {
        tbDevice.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        tbDevice.setUpdateTime(DateUtils.getNowDate());
        return toAjax(tbDeviceService.updateTbDevice(tbDevice));
    }

    @ApiOperation("补登设备信息")
    // @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping("/record")
    public AjaxResult editrecord(@RequestBody TbDevice tbDevice) {
        tbDevice.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        tbDevice.setUpdateTime(DateUtils.getNowDate());
        tbDevice.setRegistrationTime(new Date());
        return toAjax(tbDeviceService.updateTbDevice(tbDevice));
    }

    /**
     * 删除设备信息
     */
    @ApiOperation("删除设备信息")
    //@PreAuthorize("@ss.hasPermi('system:device:remove')")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbDeviceService.deleteTbDeviceByIds(ids));
    }
}
